function setCookie(cname, cvalue, exdays) {
    const d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    const expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
    const name = cname + "=";
    const decodedCookie = decodeURIComponent(document.cookie);
    const ca = decodedCookie.split(';');
    for (let i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

var app = angular.module('deepDRapp', ['ngRoute']);

app.controller('appController', ['$scope', '$timeout', '$http',
    function appController($scope, $timeout, $http) {
    $scope.name = 'default name2';
    $scope.menuItem = 'index';
    $scope.debug = false;
    $scope.token = '';
    $scope.LogedIn = false;
    $scope.set_token = function (token) {
        $scope.token = token;
        setCookie('token', token, 7);
        setCookie('name', $scope.usrname, 7)
    };
    $scope.set_menu_item = function (manu_item, callback=null) {
        // if($scope.token ===''){
        //     $scope.menuItem = 'login';
        //     return
        // }
        $scope.menuItem = manu_item;
        console.log(callback);
        if (callback!==null){
            callback();
        }
    };
    $scope.exit_login = function(){
        $scope.token = "";
        $scope.LogedIn = false;
        setCookie('token',"",0);
        setCookie('name','',0);
    };
    $scope.login = function () {
        $http({
            method: 'POST',
            url: '/api/get_token',
            headers: {
                'Content-Type': 'application/json'
            },
            data: {
                'name': $scope.usrname,
                'psw': $scope.psw
            }
        }).then(function (response) {
            // success call back
            console.log('successful request');
            console.log(JSON.stringify(response.data));
            if (response.data['token'] !== '') {
                $scope.set_token(response.data['token']);
                succ = true;
                $scope.LogedIn = true;
                alert("登录成功")
            } else {
                console.log(response.data)
            }
        }, function (response) {
            alert("登录错误！请核对用户名及密码！");
            console.log(response)
        });

    };
    $scope.init = function () {
        console.log('init');
        var token =  getCookie('token');
        $scope.usrname = getCookie('name');
        if (token !== ""){
            $scope.token = token;
            $scope.LogedIn = true;
        }
        if($scope.LogedIn){
            $http({
                method: 'GET',
                url: '/api/get_user_profile',
                headers: {
                    'Content-Type': 'application/json',
                    'token': $scope.token
                }
            }).then(function (response) {
                $scope.title = response.data.inditude;
            }, function (response) {
                alert("登录错误！");
                console.log(response)
            });        
        }
        $scope.set_menu_item('start')
    }
}]);

app.controller('newImage', ['$scope', '$timeout', '$http','$window',
    function ($scope, $timeout, $http, $window) {
        $scope.init = function () {
            $scope.user_add = {};
            $scope.user_add.name = '';
            $scope.user_add.password = '';
            $scope.user_add.title = '';
            $scope.user_add.do = function () {
              console.log("Adding user");
              data = {
                    'name': $scope.user_add.name,
                    'psw': $scope.user_add.password,
                    'inditude': $scope.user_add.title};
              console.log(data);
              $http({
                  method: 'POST',
                  url: '/api/manger/add_user',
                  headers: {
                    'Content-Type': 'application/json',
                    'token': $scope.token
                  },
                  data: data
              }).then(function (response) {
                // success call back
                console.log('successful request');
                console.log(JSON.stringify(response.data));
                alert("登录成功");
              }, function (response) {
                  alert("ERROR Please contect deceloper!");
                  console.log(response)
              });
            }
        };
        $scope.init();
    }]);


